class CardScore < ActiveRecord::Base
	belongs_to :user
	belongs_to :flashcard

	
#
# This takes a boolean value for a card and updates
# the card values.
# INPUT:	
#	true means that they got the card correct
# 	false means that they go the card incorrect
# TODO: Need to implement
  def update_card (correct)
	if correct
		self.times_correct = self.times_correct + 1
		self.last_three = 1
	else
		self.times_incorrect = self.times_incorrect + 1
		self.last_three = 0 
	end
	save
  end
  
  def self.prepare_graph (applicable_cardstats)
	applicable_cardstats.collect{ |stat|  Array["question: " + stat.flashcard.question + " answer: " + stat.flashcard.answer, stat.percent_correct] }
  end
  
  def percent_correct
	total_times = self.times_correct + self.times_incorrect
	if total_times == 0
		100
	else
		percentage = 100 * self.times_correct / total_times
	end
  end
#
# This is a convenience function to find an existing
# card_score entry or create one. 
#
  def self.get_card (user, fset, flashcard)
	card = CardScore.find(:first,  :conditions => ["user_id = ? and fset_id = ? and flashcard_id = ?", user.id, fset.id, flashcard.id])
	if card == nil
		card = CardScore.new
		card.user = user
		card.fset_id = fset.id
		card.flashcard_id = flashcard.id
		card.times_correct = 0
		card.times_incorrect = 0
		card.last_three = 0
		card.save
	end
	card
  end

  
  
end
